/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * ResultsPanel.java
 */

package panels;

import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import database.DBInterface;

/**
 * This JPanel class creates the results page for when the user tries to look up a trail.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class ResultsPanel extends JPanel
{
	private static final String[] columnTitles = {"Trail Name", "Park Name", "Terrain", "Slope", 
		"Length", "Vistas", "Difficulty", "Open"};
	
	private CardLayout cards;
	private JPanel cardPanel;
	private JTable hikerResultsTable;
	private JScrollPane scrollPane;
	
	/**
	 * This constructor takes in the cards and card panel to add this panel to the card layout
	 * @param passedInCards The card Layout
	 * @param passedInCardPanel The JPanel that holds all of the cards.
	 */
	public ResultsPanel(CardLayout passedInCards, JPanel passedInCardPanel)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInCardPanel;
		setUp();
	}
	
	/**
	 * This method creates the UI for the panel.
	 */
	void setUp()
	{
		cards.addLayoutComponent(this, MainFrame.RESULTS_PANEL);
		this.setLayout(null);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 94, 844, 340);
		this.add(scrollPane);
		
		JLabel resultsLabel = new JLabel("Results");
		resultsLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));
		resultsLabel.setBounds(378, 26, 70, 16);
		this.add(resultsLabel);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}	
	
	/**
	 * This method creates the table where the results will go.
	 * @param query The query that will get the data for the table.
	 */
	public void createResultsTable(String query)
	{
		DBInterface db = new DBInterface();
		ResultSet rs = db.customQuery(query);
		Vector<Vector<String>> data = new Vector<Vector<String>>();
		Vector<String> row;
		try 
		{
			while(rs.next())
			{
				row = new Vector<String>();
				row.add(rs.getString("trail_name"));
				row.add(rs.getString("park_name"));
				row.add(rs.getString("terrain"));
				row.add(Double.toString(rs.getDouble("slope")));
				row.add(Double.toString(rs.getDouble("length")));
				row.add(rs.getString("vista"));
				row.add(rs.getString("difficulty"));
				row.add(rs.getString("open"));
				data.add(row);
			}
			db.closeConnection();
		} 			
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
		Vector<String> columnNames = new Vector<String>();
		for(String temp : columnTitles)
		{
			columnNames.add(temp);
		}
		
		hikerResultsTable = new JTable(data, columnNames);
		
		DefaultTableCellRenderer centerAlign = new DefaultTableCellRenderer();
		centerAlign.setHorizontalAlignment(JLabel.CENTER);
		hikerResultsTable.setDefaultRenderer(String.class, centerAlign);
			
		scrollPane.add(hikerResultsTable);
		hikerResultsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		hikerResultsTable.getColumnModel().getColumn(0).setPreferredWidth(150);
		hikerResultsTable.getColumnModel().getColumn(1).setPreferredWidth(150);
		hikerResultsTable.getColumnModel().getColumn(5).setPreferredWidth(150);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setViewportView(hikerResultsTable);
	}
}
